import time
import os
from common.save_records import SaveFile
from pages.amazon2 import Amazon


def spider_amazon(excel_file_name, product_url):
    print("Task:%s..is staring" % excel_file_name)
    create_time = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))

    log_dir = "D:\\pycode\\JT_spider\\%s" % excel_file_name
    if not os.path.exists(log_dir):
        os.mkdir(log_dir)

    excel_file_full_path_name = os.path.join(
        log_dir, '%s_%s.xlsx' % (excel_file_name, create_time))
    produce_img_dir_path = "%s\\img" % log_dir

    save_files = SaveFile(excel_file_full_path_name, produce_img_dir_path)
    excel_file, excel_file_format, file_object = save_files.create_excel()
    if not os.path.exists(produce_img_dir_path):
        os.mkdir(produce_img_dir_path)

    spider = Amazon(product_url)

    row_num = 1
    for info in spider.get_all_items():
        pic_url = info[0].split("?")[0]
        img_path = save_files.download_img(pic_url=pic_url, pic_name=info[1])
        excel_file.set_row(row_num, 250)
        excel_file.insert_image(row_num, 0, img_path)
        for j in range(1, len(info)):
            excel_file.write(row_num, j, info[j], excel_file_format)
        row_num += 1
    file_object.close()


if __name__ == "__main__":
    start_time = time.time()
    info_dic = dict()
    excel_file_name1 = "amazon_christmas_graden"
    web_url1 = "https://www.amazon.com/s?k=Christmas&i=garden&rh=n%3A1055398%2Cn%3A13679381%2Cp_89%3ABest+Choice+Products&dc&qid=1616833876&rnid=1055398&ref=sr_pg_1"

    spider_amazon(product_url=web_url1, excel_file_name=excel_file_name1)
    print("Spend time: %s" % (time.time() - start_time))
